// useParticles.ts
import { useEffect, useRef } from "react";
import * as particles from "@pixi/particle-emitter";
import * as PIXI from "pixi.js";

interface UseParticlesProps {
  config: particles.EmitterConfigV3;
  image: string;
}

const useAysis = ({ config, image }: UseParticlesProps) => {
  const containerRef = useRef<PIXI.Container>(new PIXI.Container());

  useEffect(() => {
    const emitter = new particles.Emitter(containerRef.current, {
      ...config,
    });

    let elapsed = Date.now();

    const update = () => {
      requestAnimationFrame(update);

      const now = Date.now();

      emitter.update((now - elapsed) * 0.001);
      elapsed = now;
    };

    emitter.emit = true;
    update();

    return () => {
      emitter.destroy();
    };
  }, [config, image]);

  return containerRef;
};

export default useAysis;
